<template>
    <div class="inviteCreateCourseBox">
        <div class="invite-sub">
            <img class="invite-logo" src="../../static/image/icon-title.png" alt="">
        </div>

        <!-- 导航跳转栏 start-->
        <adminHeader></adminHeader>
        <!-- 导航跳转栏 end-->

        <!-- 创建课程 start -->
        <div class="inviteAll">
            <div class="public-header">
                <div class="public-header-ch">创建可邀请课程</div>
                <div class="public-header-en">CREATE COURSE</div>
                <div class="public-header-line"></div>
            </div>

            <!-- content -->
           <div class="content">
                <div class="details">
                <!-- 表单 -->
                <div class="contentLeft">
                    <div>
                        <label> <i class="red">*</i>课程名称</label>
                        <el-input v-model="createCourseData.courseName" @blur="validateData('courseName')" placeholder="请输入课程名称"></el-input>
                        <div class="emptyTip" v-show="courseNameErr">课程名称不得为空</div>
                    </div>
                    <div>
                        <label><i class="red">*</i>允许学员提前进入的时间（0.5小时-168小时）</label>
                        <el-input  v-model.number="createCourseData.earlyEnterTime" @blur="validateData('earlyEnterTime')" placeholder="请选择提前进入的时间"></el-input>
                        <div class="emptyTip" v-show="enterTimeErr">进入时间不得为空</div>
                        <div class="emptyTip" v-show="enterTimeErr1">请输入数值！</div>
                        <div class="emptyTip" v-show="enterTimeTextErr">时间范围在0.5小时-168小时之间</div>
                    </div>
                    <div>
                        <label> <i class="red">*</i>课程报名开始时间</label>
                        <div class="timeLine">
                            <el-date-picker v-model="createCourseData.singupDate" type="date" placeholder="请选择日期" @blur="validateData('singupDate')" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>                            
                            <el-time-picker 
                                style="border:none;" arrow-contro v-model="createCourseData.singupTime" value-format="HH:mm" format="HH:mm"
                                placeholder="请选择时间" @blur="validateData('singupTime')">
                            </el-time-picker>    
                        </div>
                        <div class="emptyBox">
                            <div class="emptyTip" v-show="singupDateErr">开始时间不得为空</div>
                            <div class="emptyTip" v-show="singupTimeErr">日期不得为空</div>                   
                        </div>
                    </div>
                    <div>
                        <label> <i class="red">*</i>课程报名结束时间（需提前课程开始时间24小时）</label>
                        <div class="timeLine">
                            <el-date-picker v-model="createCourseData.singupEdate" type="date" placeholder="请选择日期" @blur="validateData('singupEdate')" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>                            
                            <el-time-picker 
                                style="border:none;" arrow-contro v-model="createCourseData.singupEtime" value-format="HH:mm" format="HH:mm"
                                placeholder="请选择时间" @blur="validateData('singupEtime')" @change="enrolmentTime()">
                            </el-time-picker>    
                        </div>
                        <div class="emptyBox">
                            <div class="emptyTip" v-show="singupEdateErr">结束时间不得为空</div>
                            <div class="emptyTip" v-show="singupETimeErr">日期不得为空</div>                   
                        </div>
                    </div>
                    <div>
                        <label> <i class="red">*</i>课程开始时间</label>
                        <div class="timeLine">
                            <el-date-picker v-model="createCourseData.sDate" type="date" placeholder="请选择日期" @blur="validateData('startDate')" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>                            
                            <el-time-picker 
                                style="border:none;" arrow-contro v-model="createCourseData.sTime" value-format="HH:mm" format="HH:mm"
                                placeholder="请选择时间" @blur="validateData('startTime')">
                            </el-time-picker>    
                        </div>
                        <div class="emptyBox">
                            <div class="emptyTip" v-show="startDateErr">开始时间不得为空</div>
                            <div class="emptyTip" v-show="startTimeErr">日期不得为空</div> 
                        </div>
                    </div>
                    <div>
                        <label> <i class="red">*</i>课程结束时间</label>
                        <div class="timeLine">
                            <el-date-picker v-model="createCourseData.eDate" type="date" placeholder="请选择日期" @blur="validateData('endDate')" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
                            <el-time-picker 
                                style="border:none;" arrow-contro v-model="createCourseData.eTime" value-format="HH:mm" format="HH:mm"
                                placeholder="任意时间" @blur="validateData('endTime')">
                            </el-time-picker>
                        </div>
                        <div class="emptyBox">
                            <div class="emptyTip" v-show="endDateErr">开始时间不得为空</div>
                            <div class="emptyTip" v-show="endTimeErr">日期不得为空</div>
                            <div class="emptyTip" v-show="endTimeHaErr">结束时间不能小于开始时间</div>
                        </div>
                    </div>
                    <div>
                        <label> <i class="red">*</i>轮数</label><br/>
                        <select v-model="createCourseData.roundsNumber" class="roundsSelect" @blur="validateData('roundsNumber')" >
                            <option v-for="(list,index) in options " :value="list.value" :key="index" >{{list.value}}</option>
                        </select>
                       
                        <div class="emptyTip" v-show="roundsNumberErr">轮数不得为空</div>
                    </div>                 
                </div>
                <div class="contentLeft">
                    <div>
                        <label> <i class="red">*</i>组数</label>
                        <select v-model="createCourseData.teamsNumber" class="roundsSelect" @blur="validateData('teamsNumber')">
                            <option v-for="item in groupData" :label='item.label' :value="item.value" :key="item.id"></option>  
                        </select>
                        <div class="emptyTip" v-show="groupNumberErr">组数不得为空</div>
                    </div>
                    <div>
                        <el-checkbox label="自动运行 时间间隔" name="type" :checked="runningshow" @change="runningshow=!runningshow" ></el-checkbox>
                        <el-input v-model="createCourseData.autoRunTime" :disabled="!runningshow" @blur="validateData('autoRunTime')"></el-input>
                        <div class="emptyTip" v-show="autoRunTimeErr">时间不得为空</div>
                    </div>
                    <!-- -->
                    <div class="roundsBox" v-if="runningshow">
                        <div v-for="(list,index) in tmpRoundNum" :key="index">
                            <label><i class="red">*</i>第{{index+1}}轮</label>
                            <div class="timeLine">
                                <el-date-picker v-model="list.startDate" type="date" placeholder="选择日期" @blur="validateData('')" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>                               
                                <el-time-picker 
                                    style="border:none;" arrow-contro v-model="list.startTime" value-format="HH:mm" format="HH:mm"
                                    placeholder="任意时间" @blur="firstHandler(index)">
                                </el-time-picker>
                            </div>
                            <div class="emptyBox">
                                <div class="emptyTip" v-show="firstDateErr">日期不得为空</div>
                                <div class="emptyTip" v-show="firstTimeErr">时间不得为空</div>
                                <div class="emptyTip" v-show="endTimeHaErr">结束时间不能小于开始时间</div>
                            </div>
                        </div>
                    </div>
                    <div>
                        <label> <i class="red"></i>讲师名称</label>
                        <el-input v-model="createCourseData.lecturer" @blur="validateData('lecturer')" placeholder="请输入讲师名称（可选填）"></el-input>
                        
                    </div>
                   
                </div>
            <!-- 表单 end-->
            </div>
            <!-- content end-->

            <!-- 注 、下一步 -->
            <p class="tip">注：创建课程时间需晚于系统时间1小时</p>
            <el-button :disabled="hasError" class="nextStep" @click="createInvitations()">创建可邀请课程</el-button>
            <!-- 注 、下一步 end-->
           </div>
        </div>

        <!-- 创建成功 Dialog el-dialog  -->
        <div  class="createSuccess " v-show="centerDialogVisible">
            <!-- title="创建成功"
            :visible.sync="centerDialogVisible"
            center> -->
            <div class="successContent">
                <div class="cancel"><img class="close" src="../../static/image/close-icon.png" alt="" @click="centerDialogVisible = false"></div>
                <div class="title">创建成功</div>
                <img class="successImg" src="../../static/image/succes.png" alt="">
                <div class="curNme">课程名称：<span class="spanFont">{{courseName}}</span></div>
                <div class="timeContent">
                    <div>课程开始时间：<span class="spanFont">{{startTime}}</span></div>
                    <div>课程结束时间：<span class="spanFont">{{endTime}}</span></div>
                    <div>报名开始时间：<span class="spanFont">{{applyStartTime}}</span></div>
                    <div>报名开始时间：<span class="spanFont">{{applyEndTime}}</span></div>
                </div>
                <div class="numberOf">
                    <div>轮数：<span class="spanFont">{{roundNumber}}（轮）</span></div>
                    <div>组数：<span class="spanFont">{{teamsNumber}}（组）</span></div>
                    <div>邀请人数：<span class="spanFont">{{invitePeopleNum}}（人）</span></div>
                </div>
                <div class="invitation">
                   <div>邀请时效</div>
                   <span class="spanFont">{{inviteStartTime}} </span>
                </div>
                <div>该课程将自行运行</div>
                <div class="invitationCode">
                    <div>邀请码</div>
                    <span class="codeFont">{{inviteCode}}</span>
                    <br>
                    <span class="urlCode">{{url}}</span>
                    <div class="codeContent">
                        <img class="codeImg" :src="qrCode" alt="">
                        <div class="copy">
                            <div class="copyLink">
                                <button type="button"
                                        v-clipboard:copy="url"
                                        v-clipboard:success="onCopy"
                                        v-clipboard:error="onError"
                                >复制链接
                                </button>   
                            </div>
                            <div class="copyCode">
                                <button type="button"
                                        v-clipboard:copy="inviteCode"
                                        v-clipboard:success="onCopy"
                                        v-clipboard:error="onError"
                                >复制邀请码
                                </button>    
                            </div>
                            <!-- <div class="copyLink">复制链接</div>
                            <div class="copyCode">复制邀请码</div> -->
                        </div>
                    </div>
                </div>
                <div class="footBtn">
                    <router-link :to="{name:'initiate-invitation-page'}"><button @click="centerDialogVisible = false" class="initiateInvitation">发起邀请</button></router-link>
                    <router-link :to="{name:'curriculum-schedule'}"><button @click="centerDialogVisible = false" class="confirm">确 认</button></router-link>
                    
                </div>
            </div>
            <!-- <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false" class="initiateInvitation">发起邀请</el-button>
                <el-button class="confirm" type="primary" @click="centerDialogVisible = false">确 认</el-button>
            </span> -->
        </div>
           <!-- 创建成功 Dialog end -->
    </div>
</template>
<script>
    import adminHeader from '../components/adminHeader.vue'
    export default {
        components:{
        adminHeader:adminHeader
      },
        data(){
            return{
                //创建成功 Dialog 
                centerDialogVisible: false,
                // 临时运行轮数数组
                tmpRoundNum: [
                    {
                        roundNum: 1,
                        startDate: "",
                        startTime: ""
                    },
                    {
                        roundNum: 2,
                        startDate: "",
                        startTime: ""
                    },
                    {
                        roundNum: 3,
                        startDate: "",
                        startTime: ""
                    },
                    {
                        roundNum: 4,
                        startDate: "",
                        startTime: ""
                    },
                ],
                // 创建课程数据
                createCourseData:{
                     // 课程名称
                    courseName:"",
                    // 进入时间
                    earlyEnterTime:'',
                    // 报名开始日期
                    singupDate:'',
                    // 报名开始时间
                    singupTime:'',
                    // 报名结束日期
                    singupEdate:'',
                    // 报名开始时间
                    singupEtime:'',
                    // 报名开始日期及时间
                    applyStartTime:'',
                    // 报名结束日期及时间
                    applyEndTime:'',
                    // 开始日期
                    sDate:'',
                    // 开始时间
                    sTime:'',
                    // 结束日期
                    eDate:'',
                    // 结束时间
                    eTime:'',
                    // 开始日期及时间
                    startTime: '',
                    // 结束日期及时间
                    endTime: '',
                    // 轮数
                    roundsNumber:'',
                    // 组数
                    teamsNumber:'',
                    // 自动运行
                    // elapsedTime:'',
                    // 讲师
                    lecturer:'',
                    // 用户ID
                    createUser:0,
                    // 是否自动运行 0 不自动运行 1自动运行
                    autoRun: 0,
                    // 自动运行时间间隔
                    autoRunTime: "",
                    // 自动运行数组
                    courseRoundModels:[],
                    nickName:''
                },
                // 轮数options
                options:[
                    {
                        label:'4', value:'4'
                    },
                    {
                        label:'5', value:'5'
                    },
                    {
                        label:'6', value:'6'
                    }
                ],

                // 组数options
                groupData:[
                    {
                        label:'4', value:'4'
                    },
                    {
                        label:'5', value:'5'
                    },
                    {
                        label:'6', value:'6'
                    }
                ],
                 // 自动运行显示
                runningshow:false,
                // 创建课程空提示
                courseNameErr:false,
                enterTimeErr:false,
                enterTimeErr1:false,
                enterTimeTextErr:false,
                startDateErr:false,
                startTimeErr:false,
                endDateErr:false,
                endTimeErr:false,
                roundsNumberErr:false,
                firstDateErr:false,
                firstTimeErr:false,
                sencodDateErr:false,
                sencodTimeErr:false,
                thirdDateErr:false,
                thirdTimeErr:false,
                fourthDateErr:false,
                fourthTimeErr:false,
                autoRunTimeErr:false,
                lecturerErr:false,
                endTimeHaErr:false, // 错误文本
                hasError:false, //  是否有错
                phoneFormatErr:false, // 手机格式错误
                phoneRepeatErr:false, // 手机重复错误
                creatTipBox:false,
                singupDateErr:false,
                singupTimeErr:false,
                singupEdateErr:false,
                singupETimeErr:false,
                groupNumberErr:false,

                /**
                 * 弹窗字段
                 */
                courseName:'', // 课程名称
                startTime:'', // 课程开始时间
                endTime:'', // 课程结束时间
                applyStartTime:'', //报名开始时间 
                applyEndTime:'', //报名结束时间 
                roundNumber:'' ,// 轮数
                teamsNumber:'' ,// 组数
                invitePeopleNum:'',// 邀请人数
                inviteStartTime:'',//邀请时效
                inviteCode:'',// 邀请码
                qrCode:'',// 二维码
                url:'', // 链接
                


            }
        },
        methods:{
            // 失去焦点验证
            validateData:function(type){
                if(type == 'courseName'){
                    if(this.createCourseData.courseName == ''){
                        this.courseNameErr = true;
                    }else{
                        this.courseNameErr = false;
                    }
                };
                if(type == 'earlyEnterTime'){
                    if(this.createCourseData.earlyEnterTime == '' ){
                        this.enterTimeErr = true;
                        this.enterTimeErr1 = false;
                    } else if(!Number.parseFloat(this.createCourseData.earlyEnterTime)){
                        this.enterTimeErr = false;
                        this.enterTimeErr1 = true;
                        this.enterTimeTextErr = false;
                    }else {
                        this.enterTimeErr1 = false;
                        this.enterTimeErr = false;
                        if (!(this.createCourseData.earlyEnterTime >= 0.5 && this.createCourseData.earlyEnterTime <= 168)) {
                            this.enterTimeTextErr = true;
                        } else {
                            this.enterTimeTextErr = false;
                            this.enterTimeErr1 = false;
                        }
                        
                    }
                };
                
                // 课程开始时间
                if(type == 'singupDate'){
                    if(this.createCourseData.singupDate == ''){
                        this.singupDateErr = true;
                    }else{
                        this.singupDateErr = false;
                    }
                };
                if(type == 'singupTime'){
                    if(this.createCourseData.singupTime == ''){
                        this.singupTimeErr = true;

                    }else{
                        this.singupTimeErr = false;
                    }
                };
                // 课程结束时间 singupEdate
                if(type == 'singupEdate'){
                    if(this.createCourseData.singupEdate == ''){
                        this.singupEdateErr = true;
                    }else{
                        this.singupEdateErr = false;
                    }
                };
                if(type == 'singupEtime'){
                    if(this.createCourseData.singupEtime == ''){
                        this.singupETimeErr = true;
                    }else{
                        this.singupETimeErr = false;
                    }
                };

                if(type == 'startDate'){
                    if(this.createCourseData.sDate == ''){
                        this.startDateErr = true;
                    }else{
                        this.startDateErr = false;
                    }
                };
                if(type == 'startTime'){
                    if(this.createCourseData.sTime == ''){
                        this.startTimeErr = true;
                    }else{
                        this.startTimeErr = false;
                    }
                };
                if(type == 'endDate'){
                    if(this.createCourseData.eDate == ''){
                        this.endDateErr = true;
                    }else{
                        this.endDateErr = false;
                        if (this.createCourseData.eDate < this.createCourseData.sDate) {
                            this.endTimeHaErr = true;
                        } else {
                            this.endTimeHaErr = false;
                        }
                    }
                };
                if(type == 'endTime'){
                    if(this.createCourseData.eTime == ''){
                        this.endTimeErr = true;
                    }else{
                        this.endTimeErr = false;
                    }
                };
                if(type == 'roundsNumber'){
                    if(this.createCourseData.roundsNumber == ''){
                        this.roundsNumberErr = true;
                    }else{
                        this.roundsNumberErr = false;
                    }
                };
                if(type == 'teamsNumber'){
                    if(this.createCourseData.teamsNumber == ''){
                        this.groupNumberErr = true;
                    }else{
                        this.groupNumberErr = false;
                    }
                };
                // if(type == 'autoRunTime'){
                //     if(this.createCourseData.autoRunTime == ''){
                //         this.autoRunTimeErr = true;
                //     }else{
                //         this.autoRunTimeErr = false;
                //     }
                // };
                if(type == 'lecturer'){
                    if(this.createCourseData.lecturer == ''){
                        this.lecturerErr = true;
                    }else{
                        this.lecturerErr = false;
                    }
                };

                // 禁止下一步按钮点击
                if (
                    this.courseNameErr === false && 
                    this.enterTimeErr === false && 
                    this.startDateErr === false && 
                    this.startTimeErr === false && 
                    this.endDateErr === false && 
                    this.endTimeErr === false && 
                    this.roundsNumberErr === false && 
                    this.singupDateErr === false &&
                    this.singupTimeErr === false && 
                    this.singupEdateErr === false && 
                    this.singupETimeErr=== false && 
                    this.groupNumberErr === false &&
                    this.enterTimeTextErr == false
                    ) {
                    this.hasError = false;
                } else {
                    this.hasError = true;
                }

                
                
            },
            // 时间戳转字符串 如 1530426600 转成 "2018-07-01 14:30"
            timestampToStr(timestamp){
                let dateTime = new Date(timestamp * 1000)
                let Year = dateTime.getFullYear();
                let Month = (dateTime.getMonth()+1);
                Month = Month < 10 ? ('0' + Month) : Month;
                let Day = dateTime.getDate();
                Day = Day < 10 ? ('0' + Day) : Day;
                let Hour = dateTime.getHours();
                Hour = Hour < 10 ? ('0' + Hour) : Hour;
                let Minute = dateTime.getMinutes();
                Minute = Minute < 10 ? ('0' + Minute) : Minute;
                
                let dateArr = [Year,Month,Day];
                let timeArr = [Hour,Minute];
                
                let dateStr = dateArr.join('-');
                let timeStr = timeArr.join(':');
                return dateStr + ' ' + timeStr;
            },
                        
            // 第一轮触发的事件 (自动运行)
            firstHandler(index){
                // 判断当前是否为0，为0则是第一轮
                if (index === 0) {
                    // 计算自动间隔时间的秒数 分钟转秒数 1分钟60秒，n分钟的秒数为n*60
                    let countAutoRunTime = this.createCourseData.autoRunTime * 60;

                    // 第一轮日期时间格式的时间戳 如 "2018-07-01 14:30" 转成 1530426600
                    let firstDateTimestamp = '';

                    // 循环临时轮数
                    for (const key in this.tmpRoundNum) {
                        // 因为数组第一个的索引都为0，所以这里判断是否为第一轮
                        if (key == index) {
                            // 如果为第一轮，则把前面选择的日期和后面选择的时间拼接起来，形成如"2018-07-01 14:30"，然后转成时间戳格式如1530426600，这样就拿到第一轮的时间戳，然后后面的值都根据这个以及前面自动间隔时间转成的秒数来计算
                            firstDateTimestamp = ((new Date(this.tmpRoundNum[key].startDate + ' ' + this.tmpRoundNum[key].startTime)).getTime() / 1000)
                            // console.log(firstDateTimestamp);
                        } else {
                            // 这里必须把key转成整型，不然会报错~ :D
                            let i = parseInt(key);
                            // 这里就用自动间隔时间(秒数的) 乘以 整型的索引 就得到了 当前第n轮的时间戳啦 :) 
                            // ps:为什么要乘以索引，因为数组的第二个是1，第二轮只要乘以1次时间间隔秒数，就得到了间隔的时间戳了，那么第三轮只要乘以2次时间间隔秒数，就得到了两个间隔的时间戳了，以此类推，即第n轮乘以i(也就是key的整型)次时间间隔秒数（也可以理解为第n轮乘以n-1次，当然这里第一轮是排除的）
                            let currentAutoRunTimestamp = countAutoRunTime * i
                            // console.log(currentAutoRunTimestamp);
                            let dateTimeArr = (this.timestampToStr(firstDateTimestamp + currentAutoRunTimestamp)).split(' ');
                            // console.log(this.timestampToStr(firstDateTimestamp + currentAutoRunTimestamp))
                             this.tmpRoundNum[key].startDate = dateTimeArr[0]
                             this.tmpRoundNum[key].startTime = dateTimeArr[1]
                        }
                    }
                }
            },

            // 创建可邀请课程
            createInvitations:function(){
               let createUser = JSON.parse(sessionStorage.getItem('login')).id;
                // 1、非空验证
                for(let item in this.createCourseData){
                    if(item == 'courseName'){
                        if(this.createCourseData.courseName == ''){
                            this.courseNameErr = true;
                        }else{
                            this.courseNameErr = false;
                        }
                    };
                    if(item == 'earlyEnterTime'){
                        if(this.createCourseData.earlyEnterTime == ''){
                            this.enterTimeErr = true;
                            this.enterTimeErr1 = false
                        } else if(!Number.parseFloat(this.createCourseData.earlyEnterTime)){
                            this.enterTimeErr = false;
                            this.enterTimeErr1 = true;
                            this.enterTimeTextErr = false;
                        }else {
                            this.enterTimeErr = false;
                            this.enterTimeErr1 = false;
                            if (!(this.createCourseData.earlyEnterTime >= 0.5 && this.createCourseData.earlyEnterTime <= 168)) {
                                this.enterTimeTextErr = true;
                            } else {
                                this.enterTimeTextErr = false;
                                this.enterTimeErr1 = false;
                            }
                            
                        }
                    };
                    
                    // 课程开始时间
                    if(item == 'singupDate'){
                        if(this.createCourseData.singupDate == ''){
                            this.singupDateErr = true;
                        }else{
                            this.singupDateErr = false;
                        }
                    };
                    if(item == 'singupTime'){
                        if(this.createCourseData.singupTime == ''){
                            this.singupTimeErr = true;

                        }else{
                            this.singupTimeErr = false;
                        }
                    };
                    // 课程结束时间 singupEdate
                    if(item == 'singupEdate'){
                        if(this.createCourseData.singupEdate == ''){
                            this.singupEdateErr = true;
                        }else{
                            this.singupEdateErr = false;
                        }
                    };
                    if(item == 'singupEtime'){
                        if(this.createCourseData.singupEtime == ''){
                            this.singupETimeErr = true;
                        }else{
                            this.singupETimeErr = false;
                        }
                    };

                    if(item == 'startDate'){
                        if(this.createCourseData.sDate == ''){
                            this.startDateErr = true;
                        }else{
                            this.startDateErr = false;
                        }
                    };
                    if(item == 'startTime'){
                        if(this.createCourseData.sTime == ''){
                            this.startTimeErr = true;
                        }else{
                            this.startTimeErr = false;
                        }
                    };
                    if(item == 'endDate'){
                        if(this.createCourseData.eDate == ''){
                            this.endDateErr = true;
                        }else{
                            this.endDateErr = false;
                            if (this.createCourseData.eDate < this.createCourseData.sDate) {
                                this.endTimeHaErr = true;
                            } else {
                                this.endTimeHaErr = false;
                            }
                        }
                    };
                    if(item == 'endTime'){
                        if(this.createCourseData.eTime == ''){
                            this.endTimeErr = true;
                        }else{
                            this.endTimeErr = false;
                        }
                    };
                    if(item == 'roundsNumber'){
                        if(this.createCourseData.roundsNumber == ''){
                            this.roundsNumberErr = true;
                        }else{
                            this.roundsNumberErr = false;
                        }
                    };
                    if(item == 'teamsNumber'){
                        if(this.createCourseData.teamsNumber == ''){
                            this.groupNumberErr = true;
                        }else{
                            this.groupNumberErr = false;
                        }
                    };
                    // if(type == 'autoRunTime'){
                    //     if(this.createCourseData.autoRunTime == ''){
                    //         this.autoRunTimeErr = true;
                    //     }else{
                    //         this.autoRunTimeErr = false;
                    //     }
                    // };
                    if(item == 'lecturer'){
                        if(this.createCourseData.lecturer == ''){
                            this.lecturerErr = true;
                        }else{
                            this.lecturerErr = false;
                        }
                    };                    
                };
                // 2 、禁止“创建可邀请课程”
                if (
                    this.courseNameErr === false && 
                    this.enterTimeErr === false && 
                    this.startDateErr === false && 
                    this.startTimeErr === false && 
                    this.endDateErr === false && 
                    this.endTimeErr === false && 
                    this.roundsNumberErr === false &&
                    this.singupTimeErr === false &&
                    this.singupEdateErr === false &&
                    this.singupETimeErr === false &&
                    this.groupNumberErr === false &&
                    this.earlyEnterTime == false
                    ) 
                    {
                    this.hasError = false;
                } else {
                    this.hasError = true;
                };
                // 3 合并日期和时间
                this.createCourseData.applyStartTime = this.createCourseData.singupDate + ' ' + this.createCourseData.singupTime;
                this.createCourseData.applyEndTime = this.createCourseData.singupEdate + ' ' + this.createCourseData.singupEtime;
                this.createCourseData.startTime = this.createCourseData.sDate + ' ' + this.createCourseData.sTime;
                this.createCourseData.endTime = this.createCourseData.eDate + ' ' + this.createCourseData.eTime;
                 

                // 4、如果自动运行被勾选，则需要courseRoundModels数组
                if (this.runningshow) {
                    // 4-1创建一个空数组
                    let courseRoundModelsArr = [];
                    // 4-2循环自动运行的轮数，把日期和时间合并
                    for (const key in this.tmpRoundNum) {
                        var courseRoundModels = {
                            startTime:  this.tmpRoundNum[key].startDate + ' ' + this.tmpRoundNum[key].startTime,
                            roundNum: this.tmpRoundNum[key].roundNum
                        }
                        courseRoundModelsArr.push(courseRoundModels)
                    }

                    console.log(courseRoundModelsArr);
                    // return
                    // 4-3最后把新数组赋值给courseRoundModels
                    this.createCourseData.courseRoundModels = courseRoundModelsArr;

                    this.createCourseData.autoRun = 1;
                } else {
                    this.createCourseData.autoRun = 0;
                };
                // 接口
                
                let data = {
                    courseName : this.createCourseData.courseName,
                    earlyEnterTime :this.createCourseData.earlyEnterTime,
                    applyStartTime : this.createCourseData.applyStartTime,
                    applyEndTime : this.createCourseData.applyEndTime,
                    startTime : this.createCourseData.startTime,
                    endTime : this.createCourseData.endTime,
                    roundNumber : this.createCourseData.roundsNumber,
                    teamsNumber : this.createCourseData.teamsNumber,
                    lecturer : this.createCourseData.lecturer,
                    createUser : this.createCourseData.createUser,
                    type :1,
                    autoRun : this.createCourseData.autoRun,
                    courseRoundModelList : this.createCourseData.courseRoundModels,
                    createUser : createUser
                }
                console.log(data)
                console.log(this.createCourseData)
                this.$http.addinviteCourse(data,(res)=>{
                    if(res.errCode == 0 ){
                        this.courseName = res.data.courseName;
                        this.startTime = res.data.startTime;
                        this.endTime = res.data.endTime;
                        this.applyStartTime = res.data.applyStartTime;
                        this.applyEndTime = res.data.applyEndTime;
                        this.roundNumber = res.data.roundNumber;
                        this.teamsNumber = res.data.teamsNumber;
                        this.invitePeopleNum = res.data.invitePeopleNum;
                        this.inviteStartTime = res.data.inviteStartTime;
                        this.inviteCode = res.data.inviteCode;
                        this.qrCode = res.data.qrCode;
                        this.url = res.data.url;
                        this.centerDialogVisible = true;
                    }
                    console.log("errCode:" + res.errCode)
                    console.log(res)
                },(errRes) =>{
                    console.log(errRes)
                })
             }, 
             // 复制成功
             onCopy: function (e) {
                this.$message({
                    message: '复制成功',
                    type: 'success'
                });
                //alert('You just copied: ' + e.text)
            },
            // 复制失败
            onError: function (e) {
                this.$message({
                    message: '复制失败',
                    type: 'error'
                });
                console.log(e)
                //alert('Failed to copy texts')
            },
            // 时间戳转字符串 如 1530426600 转成 "2018-07-01 14:30"
            timestampToStr(timestamp){
                let dateTime = new Date(timestamp * 1000)
                let Year = dateTime.getFullYear();
                let Month = (dateTime.getMonth()+1);
                Month = Month < 10 ? ('0' + Month) : Month;
                let Day = dateTime.getDate();
                Day = Day < 10 ? ('0' + Day) : Day;
                let Hour = dateTime.getHours();
                Hour = Hour < 10 ? ('0' + Hour) : Hour;
                let Minute = dateTime.getMinutes();
                Minute = Minute < 10 ? ('0' + Minute) : Minute;
                
                let dateArr = [Year,Month,Day];
                let timeArr = [Hour,Minute];
                
                let dateStr = dateArr.join('-');
                let timeStr = timeArr.join(':');
                return dateStr + ' ' + timeStr;
            },
            // 日期时间字符串转时间戳 如 "2018-07-01 14:30" 转成 1530426600
            strToTimestamp (str) {
                return ((new Date(str)).getTime() / 1000)
            },

            // 课程报名结束时间判断
            enrolmentTime(){
                // 1. 拼接日期和时间 如2018-01-01 10：10：10
                let datetime = this.createCourseData.singupEdate + " " + this.createCourseData.singupEtime
                // 2. 转成时间戳 然后加上24小时也就是一天的秒数 86400
                let timestamp = this.strToTimestamp(datetime)
                console.log(timestamp)
                // 3. 下一天的时间戳
                let nexttimestamp = timestamp + 86400
                console.log(nexttimestamp)
                // 4. 再转成日期时间字符串
                let nextdatetime = this.timestampToStr(nexttimestamp)
                console.log(nextdatetime)
                // 5. 因为是两个输入框 分开的，所以要拆分一下
                let nextdate = nextdatetime.split(' ')[0]
                let nexttime = nextdatetime.split(' ')[1]
                console.log(nextdate)
                console.log(nexttime)
                // 6. 赋值给课程开始时间
                this.createCourseData.sDate = nextdate
                this.createCourseData.sTime = nexttime

            }
        },

        
    }
</script>

